﻿using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Identity;
namespace MvcCookieAuthSample.Data
{
    public class ApplicationDbContextSeed
    {
        private Microsoft.AspNetCore.Identity.UserManager<ApplicationUser> _userManager;

        public async Task SeedAsync(ApplicationDbContext context, IServiceProvider service)
        {
            using (var scope = service.CreateScope())
            {
                if (!context.Users.Any())
                {
                    _userManager = service.GetRequiredService<Microsoft.AspNetCore.Identity.UserManager<ApplicationUser>>();
                    var defaultUser = new ApplicationUser
                    {
                        UserName = "Amdinistrator",
                        Email = "18698807852@163.com",
                        NormalizedUserName = "Admin"
                    };
                    var result = await _userManager.CreateAsync(defaultUser, "Password$123");
                    if (!result.Succeeded)
                    {
                        throw new Exception("初始化用户失败");
                    }
                }
                
            }
        }
    }
}
